//用户模型
const usersModel = require('../../models/users/index');

function list(param) {
  return new Promise(async (resolve, reject) => {
    try {
      const result = await usersModel.list(param);
      resolve(result);
    } catch (error) {
      reject(error);
    };
  })
}

function byId(id) {
  return new Promise(async (resolve, reject) => {
    try {
      const todo = await usersModel.byId(id);
      if (todo._id) {
        resolve(todo);
      } else {
        reject(todo);
      }
    } catch (error) {
      reject(error);
    };
  })
}

function add(user) {
  return new Promise(async (resolve, reject) => {
    try {
      let result = await usersModel.add(user);
      if (result.id) {
        resolve(result);
      } else {
        reject(result);
      }
    } catch (error) {
      reject(error);
    };
  })
}

function edit(param) {
  return new Promise(async (resolve, reject) => {
    try {
      const result = await usersModel.edit(param);
      if (result) {
        resolve(true);
      } else {
        resolve(false);
      }
    } catch (error) {
      reject(error);
    };
  })
}

module.exports = {
  list,
  byId,
  add,
  edit
}